import QtQuick 1.0
import "../ui"
import "../components"
import "../js/twitternetworkmanager.js" as TwitterOauth
import "../style/myspace_style"
import "../js/app.js" as App
Screen {
    property alias wUrl: webView.url
    signal requestTokenReady(string authorizeUrl)
    signal accessTokenReady(string token, string tokenSecret)

    WebView {
        id: webView
        anchors.fill: parent
    }

    Item {
        id: controlsPanel

        Row {
            id: controlsRow
            anchors.right: parent.right
            height: parent.height
            spacing: App.halfMargin
            anchors.margins: App.halfMargin
            Input {
                id: input
                style: InputStyle {}
                height: parent.height - 12
                anchors.verticalCenter: parent.verticalCenter
            }

            ImageButton {
                text: "Enter PIN"
                style: ButtonStyle {}
                onClicked: {
                    TwitterOauth.getAccessToken(input.text);
                }
                height: parent.height - 10
                anchors.verticalCenter: parent.verticalCenter
            }
        }
    }

    Component.onCompleted: {
        TwitterOauth.requestToken();
    }

    onRequestTokenReady: {
        wUrl = authorizeUrl;
    }

    onAccessTokenReady: {
        //prevScreen();
    }

    QtObject {
        id: twitterPrivate
        property string statusUpdateUrl: "http://api.twitter.com/1/statuses/update.xml"
        property string method: "POST"
    }

    function postToTwitter(message) {
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function() {
            if(xhr.readyState == XMLHttpRequest.DONE) {
                switch(xhr.status) {
                case 200:
                    // successfull
                    //showNotification("Twitter update succesfull");
                    break;
                case 401:
                    // unauthorized...user revoked access to app
                    //showNotification("Unauthorized access...Please link to your twitter account again.");
                    break;
                default:
                    // other error
                    //showNotification("Twitter post failed.");
                    break;
                }
            }
        }

        var url = twitterPrivate.statusUpdateUrl

        var parameters = { status: message }

        var header = TwitterOAuth.getAuthorizationHeader(url, twitterPrivate.method, parameters);

        url += "?status=" + parameters.status;

        xhr.open(twitterPrivate.method, url);
        xhr.setRequestHeader("Authorization", header)
        xhr.send();
    }

}
